package com.lms.controller;

import com.lms.bean.*;
import com.lms.service.*;
import com.lms.util.MyStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpSession;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Controller
public class IndexController {
    @Autowired
    private RentRecordService rentRecordService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private BookService bookService;

    @Autowired
    private RequestService requestService;

    @Autowired
    private UserService userService;

    @RequestMapping("rentbook")
    public String RentBook(Long id) {
        RentRecord rentRecord = new RentRecord();
        rentRecord.setRentTime(new Date());
        //创建Calendar实例
        Calendar cal = Calendar.getInstance();
        cal.setTime(rentRecord.getRentTime());   //设置当前时间
        //增加一个月
        cal.add(Calendar.MONTH, 1);
        rentRecordService.save(rentRecord);
        return "";
    }

    //前台,zhao fan 2020 8.25

    @RequestMapping("")
    public String toIndex() {
        return "redirect:index";
    }

    @RequestMapping("index")
    public String toIndex(@RequestParam(value = "keyword", defaultValue = "") String keyword,
                          @RequestParam(value = "typeId", defaultValue = "-1") Long typeId,
                          @PageableDefault(size = 15, sort = {"bookId"}, direction = Sort.Direction.ASC) Pageable pageable,
                          Model model) {
        Page<Book> page;

        String s = StringUtils.trimAllWhitespace(keyword);
        String s1 = MyStringUtils.clear(s);
        if (-1 == typeId) {
            page = bookService.findByKeyWord(s1, pageable);
        } else {
            page = bookService.findByKeyWordWithTypeId(s1, typeId, pageable);
        }
        model.addAttribute("page", page);

        List<Type> types = typeService.findAll();
        model.addAttribute("types", types);
        model.addAttribute("keyword", keyword);

        List<Book> bookList = bookService.findByHistory();
        model.addAttribute("bookList", bookList);
        return "index";
    }

    @RequestMapping("book")
    public String toBook(Long bookId, Model model) {
        Book book = bookService.findBookById(bookId);
        System.out.println(book);
        model.addAttribute("book", book);
        return "book";
    }

    @RequestMapping("borrow")
    public String toBorrow(@PageableDefault(size = 15, sort = {"createTime"}, direction = Sort.Direction.DESC) Pageable pageable,
                           HttpSession session,Model model) {
        User user = (User)session.getAttribute("user");
        Page<Request> page = requestService.findAll(pageable,user.getUserId());

        model.addAttribute("page", page);

        return "borrow";
    }

    @RequestMapping("userInfo")
    public String toUser(Model model){
        return "user";
    }

    @RequestMapping("changeUserInfo")
    public String changeUserInfo(User user,HttpSession session){
        userService.changeUserInfo(user);
        session.setAttribute("user",userService.findUserById(user.getUserId()));
        return "redirect:index";
    }

}
